<?
require 'inc_security.php';
$action = getValue('action','str','POST','');
switch($action){
    case 'add_change_cat':
        $cat_id = getValue('catID','int','POST',0);
        //Lấy ra các event của category này
        $db_event = new db_query('SELECT * FROM events WHERE eve_cat_id = '.$cat_id);
        ?>
        <option>-- Chọn event --</option>
        <?
        while($row = mysql_fetch_assoc($db_event->result)){
            ?>
            <option value="<?=$row['eve_id']?>"><?=$row['eve_title']?></option>
            <?
        }
        unset($db_event);
        break;
    case 'add_image':
        //Chèn ảnh cá nhân : pga_type = 0
        $db_sl_image = new db_query('SELECT * FROM private_galleries WHERE pga_type = 0 AND pga_admin_id = '.$admin_id. ' ORDER BY pga_date DESC LIMIT 10');
        $slimg = $db_sl_image->resultArray();unset($db_sl_image);
        ?>
        <div class="popup-header">Add Image</div>
        <div class="popup-content">
            <table>
                <tr>
                    <td class="form_name">Chọn ảnh : </td>
                    <td class="form_text">
                        <div style="width: 500px;height: 270px;overflow: auto;margin-left: 22px;">
                        <? if($slimg){
                            foreach($slimg as $img):
                            ?>
                            <div style="float: left;margin-right:10px;margin-bottom:10px" class="form_control">
                                <label for="image_<?=$img['pga_id']?>"><img src="../../../pictures/private_galleries/<?=$img['pga_content']?>" title="<?=$img['pga_name']?>" style="width:80px;max-height:100px;"/></label>
                                <span><input type="checkbox" class="private_img" name="image_<?=$img['pga_id']?>" id="image_<?=$img['pga_id']?>" value="<?=$img['pga_id']?>"/></span>
                            </div>
                            <?
                            endforeach;
                        }?>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="form_name">Kích thước :</td>
                    <td class="form_text">
                        &nbsp;&nbsp;&nbsp;
                        <label>Rộng :</label>
                        <input type="text" class="form_control gallery_width" style="width:30px;text-align:right;" value="0" />
                        <label>px</label>
                        &nbsp;
                        <label>Dài :</label>
                        <input type="text" class="form_control gallery_height" style="width:30px;text-align:right;" value="0" />
                        <label>px</label>
                    </td>
                </tr>
                <tr>
                    <td class="form_name"></td>
                    <td class="form_text">
                        <button type="button" class="create_code">Tạo code</button>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td class="form_text">
                        <div class="code_build"></div>
                        <script>
                        $('.create_code').click(function(){
                            pImg = [];
                            $('.private_img').each(function(){
                                //console.log($(this).attr('checked'));
                                if($(this).attr('checked') == 'checked'){
                                    pImg.push($(this).val());
                                }
                            });
                            data = {action:'create_code_img',
                                      pImg:pImg,
                                      w:$('.gallery_width').val(),
                                      h:$('.gallery_height').val()
                                    };
                            $.ajax({
                                type:'post',
                                url:'ajax.php',
                                data:data,
                                success:function(html){
                                    $('.code_build').html(html);
                                } 
                            })
                        });
                        </script>
                    </td>
                </tr>
            </table>
            
        </div>
        <?
        break;
        
        /***************/
    case 'add_slide':
        /* Thêm slide cho các tin khác */
        /*
        $cat_id = getValue('catID','int','POST',0);
        $db_cat_type = new db_query('SELECT cat_type FROM categories_multi WHERE cat_id = '.$cat_id);
        $cat_type = mysql_fetch_assoc($db_cat_type->result);unset($db_cat_type);
        $cat_type = $cat_type['cat_type'];
        if($cat_type != 'news'){
            break;
        }
        //Điểm tin thì cho thêm slide
        */
        ?>
        <div class="sl_type">
            <input type="radio" name="slide_type" class="slide_type" id="slide_type0" value="0"/>
            <label for="slide_type0">Không có slide</label>&nbsp;&nbsp;
            <input type="radio" name="slide_type" class="slide_type" id="slide_type1" value="1"/>
            <label for="slide_type1">1 ảnh</label>&nbsp;&nbsp;
            <input type="radio" name="slide_type" class="slide_type" id="slide_type3" value="3"/>
            <label for="slide_type3">3 ảnh</label>&nbsp;&nbsp;
        </div>
        <div class="slide_file">
            
        </div>
        <script>
        $('.slide_type').click(function(){
            type = $(this).val();
            if(type == 1){
                $('.slide_file').html('');
                $divct = '<input type="hidden" name="slide_record[]" value="1"/>';
                $divct += '<input type="file" name="slide_file_1"/>';
                $('.slide_file').append($divct);
            }
            if(type == 3){
                $('.slide_file').html('');
                for(i=1;i<=3;i++){
                    $divct = '<input type="hidden" name="slide_record[]" value="'+i+'"/>';
                    $divct += '<input type="file" name="slide_file_'+i+'"/>';
                    $('.slide_file').append($divct);
                }
            }
            if(type == 0){
                $('.slide_file').html('');
            }
        })
        </script>
        <?
        break;
    case 'add_pitching':
        $cat_id = getValue('catID','int','POST',0);
        $db_pitching = new db_query('SELECT * FROM pitching WHERE pit_cat_id = '.$cat_id.' ORDER BY pit_order ASC');
        ?>
        <option>-- Chọn pitching --</option>
        <?
        while($row = mysql_fetch_assoc($db_pitching->result)){
            ?>
            <option value="<?=$row['pit_id']?>"><?=$row['pit_name']?></option>
            <?
        }
        unset($db_pitching);
        break;
        
        
    case 'add_gallery':
            $db_gallery = new db_query('SELECT gal_id,gal_name,gal_date FROM galleries ORDER BY gal_name ASC, gal_date DESC');
            $galleries = $db_gallery->resultArray();unset($db_gallery);
            ?>
            <div class="popup-header">Add Gallery</div>
            <div class="popup-content">
            <table>
                <tr>
                    <td class="form_name">Chọn bộ sưu tập :</td>
                    <td class="form_text">
                        <select name="gallery_id" id="gallery_id">
                            <option>-- Chọn --</option>
                            <?foreach($galleries as $gallery):?>
                            <option value="<?=$gallery['gal_id']?>"><?=$gallery['gal_name']?> - Ngày tạo : <?=date('d/m/y',$gallery['gal_date'])?></option>
                            <?endforeach;?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="form_name">Kích thước : </td>
                    <td class="form_text">
                        &nbsp;&nbsp;&nbsp;
                        <label>Rộng :</label>
                        <input type="text" class="form_control gallery_width" style="width:30px;text-align:right;" value="0" />
                        <label>px</label>
                        &nbsp;
                        <label>Dài :</label>
                        <input type="text" class="form_control gallery_height" style="width:30px;text-align:right;" value="0" />
                        <label>px</label>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td class="form_text">
                        <br />
                        <button type="button" class="create_code" style="padding: 5px 7px;">Tạo code</button>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td class="form_text">
                        <div class="code_build"></div>
                        <script>
                        $('.create_code').click(function(){
                            data = {action:'create_code',
                                      gID:$('#gallery_id').val(),
                                      w:$('.gallery_width').val(),
                                      h:$('.gallery_height').val()
                                    };
                            $.ajax({
                                type:'post',
                                url:'ajax.php',
                                data:data,
                                success:function(html){
                                    $('.code_build').html(html);
                                } 
                            })
                        });
                        </script>
                    </td>
                </tr>
            </table>
            </div>
            
            <?
        break;
        
            
    case 'create_code_img':
        $pImg = getValue('pImg','arr','POST',0);
        if(!$pImg){
            break;
        }
        $width = getValue('w','int','POST',0);
        $height = getValue('h','int','POST',0);
        $db_gallery = new db_query('SELECT * FROM private_galleries WHERE pga_type = 0 AND pga_id IN ('.implode(',',$pImg).')');
        $gallery = $db_gallery->resultArray();
        
        ?>
        <textarea style="width: 500px;height:80px" id="select_code"><div class="vcamp-images"><?foreach($gallery as $apic):?><img src="/pictures/private_galleries/<?=$apic['pga_content']?>" style="width: <?=$width ? $width.'px' : 'auto'?>;height:<?=$height ? $height.'px' : 'auto'?>"/><?endforeach?></div></textarea>
        <div><font class="form_asterisk">*</font> Copy đoạn mã trên và dán vào bài viết</div>
        <?
        break;
    
    case 'create_code':
            $gID = getValue('gID','int','POST',0);
            if(!$gID){
                break;
            }
            $width = getValue('w','int','POST',0);
            $height = getValue('h','int','POST',0);
            $db_gallery = new db_query('SELECT * FROM galleries WHERE gal_id = '.$gID);
            $gallery = mysql_fetch_assoc($db_gallery->result);
            $pictures = json_decode(base64_decode($gallery['gal_content']),1);
            uasort($pictures,'sort_by_order_array_asc');
            ?>
            <textarea style="width: 400px;height:80px" id="select_code"><div class="vcamp-gallery" data-gallery-id="vcamp-gallery-<?=$gallery['gal_id']?>" style="width: <?=$width ? $width.'px' : '600px'?>;height:<?=$height ? $height.'px' : '350px'?>"><?foreach($pictures as $pic):?><img src="/pictures/galleries/<?=$pic['picture']?>" data-description="<?=$pic['info']?>"/><?endforeach;?></div></textarea>
            <div><font class="form_asterisk">*</font> Copy đoạn mã trên và dán vào bài viết</div>
            <?
        break;
    
    case 'delHistory':
        $new_id = getValue('id','int','POST',0);
        $db_del = new db_execute('DELETE FROM history_edit WHERE his_new_id = '.$new_id);
        unset($db_del);
        break;
    case 'search_news_relate':
        $keyword = getValue('kw','str','POST','');
        if(!$keyword){
            break;
        }
        $db_se = new db_query('SELECT new_id,new_title FROM news LEFT JOIN categories_multi ON new_cat_id = cat_id
                                WHERE cat_active = 1 AND new_status = 6 AND (new_title LIKE "%'.$keyword.'%" OR new_tags LIKE "%'.$keyword.'%") 
                                ORDER BY new_title ASC 
                                LIMIT 20 ' );
        $result = $db_se->resultArray();unset($db_se);
        $result = array_unique($result,SORT_REGULAR);
        if($result){
        foreach($result as $lir):
        ?>
        <li>
            <input type="checkbox" title="<?=$lir['new_title']?>" value="<?=$lir['new_id']?>" class="new_relate_result" id="search_news_relate_<?=$lir['new_id']?>"/>&nbsp;
            <label for="search_news_relate_<?=$lir['new_id']?>"><b><?=$lir['new_title']?></b></label>
        </li>
        <?
        endforeach;
        ?>
        <button class="add_news_relate" type="button" onclick="addNewsRelate()">Thêm vào</button>
        <?
        }
        break;
    case 'search_news_recommend':
        $keyword = getValue('kw','str','POST','');
        if(!$keyword){
            break;
        }
        $db_se = new db_query('SELECT new_id,new_title FROM news LEFT JOIN categories_multi ON new_cat_id = cat_id
                                WHERE cat_active = 1 AND new_status = 6 AND (new_title LIKE "%'.$keyword.'%" OR new_tags LIKE "%'.$keyword.'%") 
                                ORDER BY new_title ASC 
                                LIMIT 20 ' );
        $result = $db_se->resultArray();unset($db_se);
        $result = array_unique($result,SORT_REGULAR);
        if($result){
        foreach($result as $lir):
        ?>
        <li>
            <input type="checkbox" title="<?=$lir['new_title']?>" value="<?=$lir['new_id']?>" class="new_recommend_result" id="search_news_recommend_<?=$lir['new_id']?>"/>&nbsp;
            <label for="search_news_recommend_<?=$lir['new_id']?>"><b><?=$lir['new_title']?></b></label>
        </li>
        <?
        endforeach;
        ?>
        <button class="add_news_recommend" type="button" onclick="addNewsRecommend()">Thêm vào</button>
        <?
        }
        break;
    
    case 'update_slot':
        $new_id = getValue('id','int','POST',0);
        $slot = getValue('slot','int','POST',0);
        $db_update = new db_execute('UPDATE news SET new_order_top = 0 WHERE new_order_top = '.$slot);
        $db_update = new db_execute('UPDATE news SET new_order_top = '.$slot.' WHERE new_id = '.$new_id);
        break;
}
?>